
import router from './router'
import store from './store'
import nProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

// 路由导航守卫
// 前置导航守卫
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  nProgress.start()// 开启进度条
  const token = store.getters.token

  if (token) {
    if (to.path === '/login') {
      next('/')
      nProgress.done()// 关闭进度条
    } else {
      // 1.判断是否获取过资料
      // 如果没有派发仓库中的action事件
      if (!store.getters.userId) {
        await store.dispatch('user/updataUserInfoSync')
      }

      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nProgress.done()// 关闭进度条
    }
  }
})
// 后置导航守卫
router.afterEach(() => {
  nProgress.done()// 关闭进度条
})
